home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 56194 / 56194.xpi / chrome / topsite.jar / content / js / dal.js < prev    next >
Text File  |  2010-01-14  |  11KB  |  247 lines

  1. if (!topsite.dal) topsite.dal = {};
  2.  
  3. topsite.dal = {
  4.     serverUrl: "http://similarnet.com/similarweb/TopSitesWebGetService.svc",
  5.     lb1Url: "http://similarnet.com/LoadBalancer/Main.asmx",
  6.     lb2Url: "",
  7.     serverSessionId: "TF12345678",
  8.     initialized: false,
  9.  
  10.     init: function(){
  11.         if (topsite.dal.initialized)
  12.             return;
  13.         try{
  14.         topsite.dal.lb2Url = topsite.common.prefManager.getCharPref(topsite.consts.PREF_SECOND_LB); 
  15.         }catch(e){}
  16.         if (topsite.dal.lb2Url){
  17.             topsite.dal.getDataUrl();
  18.         }else{
  19.             topsite.dal.getLb2Url();
  20.         }
  21.     },
  22.  
  23.     getLb2Url: function(){    
  24.                 var myVersion = topsite.common.getAddonVersion();
  25.  
  26.                 var params = "";
  27.                 params += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  28.                 params += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">";
  29.                 params +=   "<soap:Body>";
  30.                 params +=     "<FirstTimeGetConnectionDetails xmlns=\"Search.SimilarWeb.com/ConnectFirstTime\">";
  31.                 params +=       "<request>";
  32.                 params +=         "<VersionNumber>" + myVersion + "</VersionNumber>";
  33.                 params +=       "</request>";
  34.                 params +=     "</FirstTimeGetConnectionDetails>";
  35.                 params +=   "</soap:Body>";
  36.                 params += "</soap:Envelope>";
  37.             
  38.                 var action = "Search.SimilarWeb.com/ConnectFirstTime/FirstTimeGetConnectionDetails";            
  39.                 topsite.dal.QueryToSoap(topsite.dal.lb1Url, params, action, topsite.dal.getSecondLBResponse);        
  40.         },
  41.     
  42.         getSecondLBResponse: function(responseXML){
  43.             topsite.dal.lb2Url = responseXML.getElementsByTagName("ServerDetails")[0].firstChild.nodeValue;           
  44.             if (topsite.dal.lb2Url){
  45.                     topsite.common.prefManager.setCharPref(topsite.consts.PREF_SECOND_LB, topsite.dal.lb2Url);            
  46.                     topsite.dal.getDataUrl();
  47.             }
  48.         },
  49.  
  50.     getDataUrl: function(){
  51.         var myVersion = topsite.common.getAddonVersion();
  52.             
  53.                 var params = "";
  54.                 params += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  55.                 params += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">";
  56.                 params +=   "<soap:Body>";
  57.                 params +=     "<GetConnectionDetails xmlns=\"Search.SimilarWeb.com/Connect\">";
  58.                 params +=       "<request>";
  59.                 params +=         "<VersionNumber>" + myVersion + "</VersionNumber>";
  60.                 params +=       "</request>";
  61.                 params +=     "</GetConnectionDetails>";
  62.                 params +=   "</soap:Body>";
  63.                 params += "</soap:Envelope>";
  64.             
  65.                 var action = "Search.SimilarWeb.com/Connect/GetConnectionDetails";
  66.                 topsite.dal.QueryToSoap(topsite.dal.lb2Url, params, action, topsite.dal.getDataWsResponse);                    
  67.         },
  68.  
  69.         getDataWsResponse: function(responseXML){                        
  70.                 topsite.dal.serverUrl = responseXML.getElementsByTagName("ServerDetails")[0].firstChild.nodeValue;                                        
  71.         topsite.dal.serverUrl = topsite.dal.serverUrl.substring(0, topsite.dal.serverUrl.lastIndexOf("/") + 1) + "TopSitesWebGetService.svc";
  72.         topsite.dal.serverSessionId = responseXML.getElementsByTagName("SessionID")[0].firstChild.nodeValue;        
  73.     },
  74.  
  75.     getTopicsForUrl: function(strUrl, prevUrl, navType, isFromLink, cancelAdv, callback, paramsForCallback){        
  76.         var topicsFromCache = this.getTopicsFromCache(strUrl);        
  77.         if (topicsFromCache != null){
  78.             callback(topicsFromCache, paramsForCallback);
  79.             return;
  80.         }
  81.         topsite.dal.getTopicsFromServer(strUrl, prevUrl, navType, isFromLink, cancelAdv, callback, paramsForCallback);
  82.     },
  83.     
  84.     getTopSiteForTopic: function(strTopic, strUrl, callback, paramsForCallback){
  85.         var topSiteFromCache = this.getTopSiteFromCache(strTopic, strUrl);
  86.         if (topSiteFromCache != null){
  87.             callback(topSiteFromCache, paramsForCallback);
  88.             return;
  89.         }
  90.         topsite.dal.getTopSiteFromServer(strTopic, strUrl, callback, paramsForCallback);
  91.     },
  92.  
  93.     getTopicsFromServer: function(strUrl, prevUrl, navType, isFromLink, cancelAdv, callback, paramsForCallback){
  94.         var params = "";
  95.             
  96.                 params += "NewSite=" + escape(strUrl) + "&";
  97.                 params += "SessionID=" + this.serverSessionId + "&";
  98.                 params += "GSessionID=" + topsite.common.getGSessionId() + "&";
  99.                 params += "IsLink=" + isFromLink + "&";
  100.                 if (cancelAdv)
  101.             params += "AdvType=-1&";
  102.                 params += "NavigatedType=" + navType + "&";
  103.                 params += "PreviousSite=" + escape(prevUrl) + "&";
  104.                 params += "SourceID=500";
  105.             
  106.         topsite.dal.QueryToSimpleXml(topsite.dal.serverUrl + "/GetTopics", params, topsite.dal.getTopicsResponse, callback, paramsForCallback, strUrl);
  107.     },
  108.     
  109.     getTopSiteFromServer: function(strTopic, strCurrUrl, callback, paramsForCallback){
  110.         var params = "";
  111.  
  112.         params += "Topic=" + escape(strTopic) + "&";
  113.                 params += "GSessionID=" + topsite.common.getGSessionId() + "&";
  114.                 params += "SessionID=" + topsite.dal.serverSessionId + "&";
  115.                 params += "Url=" + escape(strCurrUrl) + "&";                
  116.                 params += "SourceID=500&";        
  117.                 params += "NavigatedType=501";
  118.         
  119.         var arrTopicUrl = new Array(2);
  120.         arrTopicUrl[0] = strTopic;
  121.         arrTopicUrl[1] = strCurrUrl;
  122.         topsite.dal.QueryToSimpleXml(topsite.dal.serverUrl + "/GetTopSites", params, topsite.dal.getTopSiteResponse, callback, paramsForCallback, arrTopicUrl);
  123.     },
  124.  
  125.     getTopicsResponse: function(responseXML, callback, paramsForCallback, strUrl){
  126.  
  127.         var arrTopicElements = responseXML.firstChild.childNodes;
  128.  
  129.         var arrTopicsForUrl = new Array(arrTopicElements.length);
  130.         for (var i = 0; i < arrTopicElements.length; ++i){
  131.             arrTopicsForUrl[i] = arrTopicElements[i].firstChild.nodeValue;
  132.         }        
  133.         callback(arrTopicsForUrl, paramsForCallback);
  134.         topsite.dal.setTopicsToCache(strUrl, arrTopicsForUrl);
  135.     },
  136.  
  137.     getTopSiteResponse: function(responseXML, callback, paramsForCallback, arrTopicUrl){
  138.  
  139.         var arrUrlElements = responseXML.firstChild.childNodes;
  140.         var arrUrlsForTopic = new Array(arrUrlElements.length);
  141.  
  142.         for (var i = 0; i < arrUrlElements.length; ++i){
  143.             arrUrlsForTopic[i] = new Array(3);
  144.             arrUrlsForTopic[i][0] = arrUrlElements[i].getElementsByTagName("Url")[0].firstChild.nodeValue;
  145.             arrUrlsForTopic[i][1] = arrUrlsForTopic[i][0];
  146.             arrUrlsForTopic[i][2] = false;
  147.             if (arrUrlElements[i].getElementsByTagName("NavigateUrl").length>0 && arrUrlElements[i].getElementsByTagName("NavigateUrl")[0].firstChild && arrUrlElements[i].getElementsByTagName("NavigateUrl")[0].firstChild.nodeValue){
  148.                 arrUrlsForTopic[i][1] = arrUrlElements[i].getElementsByTagName("NavigateUrl")[0].firstChild.nodeValue;
  149.                 arrUrlsForTopic[i][2] = true;
  150.             }
  151.         }        
  152.         callback(arrUrlsForTopic, paramsForCallback);
  153.         topsite.dal.setTopSiteToCache(arrTopicUrl[0], arrTopicUrl[1], arrUrlsForTopic);
  154.     },
  155.  
  156.     getTopicsFromCache: function(strUrl){
  157.         // TODO: implement
  158.         return null;
  159.     },
  160.  
  161.     getTopSiteFromCache: function(strTopic, strUrl){
  162.         // TODO: implement
  163.         return null;
  164.     },
  165.  
  166.     setTopicsToCache: function(strUrl, arrTopicsForUrl){
  167.         // TODO: implement
  168.     },
  169.     
  170.     setTopSiteToCache: function(strTopic, strUrl, arrSitesForTopic){
  171.         // TODO: implement
  172.     },
  173.     
  174.     QueryToSoap: function(url, soapParams, soapAction, innerCallback, callback, paramsForCallback, innerParams){
  175.                 var http = new XMLHttpRequest();                 
  176.                     http.open("POST",url,true);
  177.             
  178.         if (http.channel instanceof Components.interfaces.nsISupportsPriority){                    
  179.             // Set the priority to lower (positive is lower)
  180.             http.channel.adjustPriority(999);              
  181.                     }
  182.             
  183.                     http.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
  184.                     http.setRequestHeader("Content-Length", soapParams.length);
  185.         http.setRequestHeader("SOAPAction", soapAction);
  186.             
  187.                     var httpAborted = false;            
  188.                     var requestTimer = setTimeout(function() {
  189.                           httpAborted = true;
  190.                                 http.abort();
  191.                        }, 3000);
  192.         
  193.         http.onreadystatechange = function handleResponse(){       
  194.                                                        if(http.readyState==4){
  195.                                         clearTimeout(requestTimer);
  196.                                         var status = http.status;
  197.                             
  198.                                         if(status==200){
  199.                                                 innerCallback(http.responseXML, callback, paramsForCallback, innerParams);
  200.                                         }
  201.                                         else{
  202.                                                 // Request with no response
  203.                                                 if (status!=202){
  204.                                                      // TODO: handle error
  205.                                                 }
  206.                                         }
  207.                                 }
  208.                         }
  209.             
  210.         http.send(soapParams);                    
  211.     },
  212.  
  213.     QueryToSimpleXml: function(url, params, innerCallback, callback, paramsForCallback, innerParams){
  214.                 var http = new XMLHttpRequest();
  215.             
  216.                     http.open("GET",url + "?" + params,true);
  217.             
  218.         if (http.channel instanceof Components.interfaces.nsISupportsPriority){                    
  219.             // Set the priority to lower (positive is lower)
  220.             http.channel.adjustPriority(999);              
  221.                     }
  222.             
  223.                         var httpAborted = false;            
  224.                     var requestTimer = setTimeout(function() {
  225.                           httpAborted = true;
  226.                                 http.abort();
  227.                        }, 3000);
  228.         
  229.         http.onreadystatechange = function handleResponse(){       
  230.                                                        if(http.readyState==4){
  231.                                         clearTimeout(requestTimer);
  232.                                         var status = http.status;
  233.                                         if(status==200){
  234.                                                 innerCallback(http.responseXML, callback, paramsForCallback, innerParams);
  235.                                         }
  236.                                         else{
  237.                                                 // Request with no response
  238.                                                 if (status!=202){
  239.                                                      // TODO: handle error
  240.                                                 }
  241.                                         }
  242.                                 }
  243.                         }
  244.             
  245.         http.send(null);                    
  246.     }
  247. };